#coding:utf-8
import os
import hashlib
from django.db import models
from lovewith.api.qiniu import Qiniu
from lovewith.utils.mtcache import MtCache


#站点设置
class Website(models.Model):
    title = models.CharField(max_length=255, blank=True, verbose_name=u'标题')
    keyword = models.CharField(max_length=255, blank=True, verbose_name=u'关键词')
    description = models.CharField(max_length=255, blank=True, verbose_name=u'描述')

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name_plural = u'SEO设置'
        db_table = 'mt_website'


#友情链接
class Link(models.Model):
    site_name = models.CharField(max_length=30, verbose_name=u'网站名')
    site_url = models.CharField(max_length=75, verbose_name=u'网站地址')
    sort = models.SmallIntegerField(default=0, verbose_name=u'排序')

    def __unicode__(self):
        return self.site_name

    class Meta:
        verbose_name_plural = u'友情链接'
        db_table = 'mt_link'


#color item
class Color(models.Model):
    sort = models.SmallIntegerField(default=0, verbose_name=u'排序')
    name = models.CharField(max_length=20, verbose_name=u'名称')
    value = models.CharField(max_length=7, default='#', verbose_name=u'颜色')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = u'灵感颜色'
        db_table = 'mt_color'


#角色
class Role(models.Model):
    name = models.CharField(max_length=13, verbose_name=u'角色')
    sort = models.SmallIntegerField(default=0, verbose_name=u'排序')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = u'商家角色'
        db_table = 'mt_role'

    def save(self):
        super(Role, self).save()
        #清空缓存
        MtCache('role').delete()


#计算类目图片上传路径
def get_cate_upload_path(instance, filename):
    file_ext = os.path.splitext(filename)[1]
    new_name = hashlib.new('md5', filename).hexdigest()
    return 'upload.tmp/%s%s' % (new_name, file_ext)


#cate
class Category(models.Model):
    name = models.CharField(max_length=45, verbose_name=u'名称')
    parent = models.ForeignKey('self', blank=True, null=True, verbose_name=u'上一级')
    sort = models.SmallIntegerField(default=0, verbose_name=u'排序')
    banner = models.ImageField(upload_to=get_cate_upload_path, blank=True, null=True, verbose_name=u'类目banner')
    keywords = models.TextField(blank=True, verbose_name=u'关键词')
    description = models.TextField(blank=True, verbose_name=u'描述')

    def __unicode__(self):
        return self.name

    def save(self):
        super(Category, self).save()

        #保存图片到upyun
        if self.banner.path.find('http') < 0:
            promote_banner = Qiniu('mt-other').upload(self.banner.path)
            if promote_banner:
                self.banner = promote_banner['path']
                super(Category, self).save()

    class Meta:
        verbose_name_plural = u'灵感分类'
        db_table = 'mt_category'


#省份
class Province(models.Model):
    name = models.CharField(max_length=45, verbose_name=u'省')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = u'省'
        db_table = 'mt_province'


#城市
class City(models.Model):
    province = models.ForeignKey(Province, verbose_name=u'省')
    name = models.CharField(max_length=45, verbose_name=u'市')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = u'市'
        db_table = 'mt_province_city'


#微信公众平台
class WeixinMp(models.Model):
    content = models.TextField(verbose_name=u'数据设置')

    def __unicode__(self):
        return u'微信公众平台自动回复数据'

    class Meta:
        verbose_name_plural = u'微信公众平台'
        db_table = 'mt_weixin'
